package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.WorkProcessDetailAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ProcessContent;
import com.isunland.managesystem.entity.WorkProcessDetailList;
import com.isunland.managesystem.entity.WorkProcessDetailList.ProcessRegistDetailContent;
import java.util.ArrayList;
import java.util.HashMap;

import static com.isunland.managesystem.ui.WorkProcessEditFragment.EXTRA_PROCESS_CONTENT;

/**
 * 工作计划登记列表
 *
 * @author wu
 */
public class WorkProcessDetailListFragment extends BaseListFragment {

  HashMap<String, String> mProcessDetail;
  //public static final String EXTRA_ITEM = "com.isunland.managesystem.ui.WorkProcessDetailListFragment.EXTRA_ITEM";
  //private List<ProcessRegistDetailContent> mProcessDetailContent;
  private WorkProcessDetailAdapter mAdapter;
  private String mId;
  private ProcessContent mItem;

  public static WorkProcessDetailListFragment newInstance(ProcessContent item) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_PROCESS_CONTENT, item);
    WorkProcessDetailListFragment fragment = new WorkProcessDetailListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //		mId=SharedPreferencesUtil.getString(getActivity(), SharedPreferencesUtil.MAIN_ID,"");
    mItem = (ProcessContent) getArguments().getSerializable(EXTRA_PROCESS_CONTENT);
    if (mItem != null) {
      mId = mItem.getId();
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_PLAN_DETAIL_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    mProcessDetail = new HashMap<>();
    mProcessDetail.put("curPage", "1");
    mProcessDetail.put("pageSize", "10000");
    mProcessDetail.put("mainId", mId != null ? mId : "");

    return mProcessDetail;
  }

  @Override public void resolveResponse(String arg0) {
    WorkProcessDetailList processDetialList =
        new Gson().fromJson(arg0, WorkProcessDetailList.class);
    ArrayList<ProcessRegistDetailContent> processDetail = processDetialList.getDataList();
    setAdapter(processDetail);
  }

  private void setAdapter(ArrayList<ProcessRegistDetailContent> processDetail) {
    //if (mProcessDetailContent == null) {
    //    mProcessDetailContent = new ArrayList<>();
    //}
    mAdapter = new WorkProcessDetailAdapter(mActivity, processDetail);
    setListAdapter(mAdapter);
    ((WorkProcessDetailAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    ProcessRegistDetailContent contentDetail = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), WorkProcessDetailActivity.class);
    intent.putExtra(WorkProcessDetailFragment.EXTRA_VALUE, contentDetail);
    intent.putExtra(WorkProcessDetailFragment.EXTRA_TASK, mItem);
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }

  // 刷新列表
  public void refresh() {
    volleyPost();
  }
}
